package com.Service;

import com.Untils.SqlSessionFactoryUtils;
import com.mapper.userMapper;
import com.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserService {
    private  SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.sqlSessionFactoryget();

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public  User login(String username,String password)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        User user = mapper.select(username,password);

        sqlSession.close();

        return user;
    }

    /**
     * 查询
     */

    public User selectById(String username)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        User user = mapper.selectByUsername(username);

        sqlSession.close();

        return user;
    }

    public void add(User user)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        mapper.addUser(user);

        sqlSession.commit();

        sqlSession.close();

    }
}
